package com.jiaoyou.baojian.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.activity.LoginRegActivity;
import com.jiaoyou.baojian.activity.UserInfoActivity;
import com.jiaoyou.baojian.adapter.RankAdapter;
import com.jiaoyou.baojian.entity.UserInfo;
import com.jiaoyou.baojian.entity.response.RankListResponse;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback;
import com.jiaoyou.baojian.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit2.Call;

public class RankListFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
    private static final String TYPE = "rankType";     //类型 CONTRIBUTION/ATTRACTIVENESS 对应 土豪榜/魅惑榜
    private static final String RANGE = "rankRange";   //时间范围 DAILY/WEEKLY/MONTHLY/TOTAL 对应 日榜/周榜/月榜/总榜
    Unbinder unbinder;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private String rankType;
    private String rankRange;
    private RankAdapter rankAdapter;

    private RankListResponse rankListResponse;

    public RankListFragment() {
    }

    public static RankListFragment newInstance(String rankType, String rankRange) {
        RankListFragment fragment = new RankListFragment();
        Bundle args = new Bundle();
        args.putString(TYPE, rankType);
        args.putString(RANGE, rankRange);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            rankType = getArguments().getString(TYPE);
            rankRange = getArguments().getString(RANGE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    private View getEmptyView() {
        TextView textView = new TextView(getContext());
        textView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        textView.setGravity(Gravity.CENTER);
        textView.setText("没有数据");
        return textView;
    }

    private void getData(){
        Api.getInstance().getRankList(rankType, rankRange, new BaofangCallback<RankListResponse>() {
            @Override
            public void onFailure(Call call, Throwable t) {
                toast("获取数据错误：" + t.getLocalizedMessage());
                swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onSuccess(RankListResponse rankListResponse) {
                RankListFragment.this.rankListResponse = rankListResponse;
                if("CONTRIBUTION".equals(rankType)) {
                    rankAdapter = new RankAdapter(rankListResponse.rankList, 2);
                } else if("ATTRACTIVENESS".equals(rankType)) {
                    rankAdapter = new RankAdapter(rankListResponse.rankList, 1);
                }
                rankAdapter.setUserInfoList(rankListResponse.userInfoList);
                rankAdapter.setOnItemClickListener(RankListFragment.this);
                rankAdapter.setEmptyView(getEmptyView());
                recyclerview.setAdapter(rankAdapter);
                rankAdapter.notifyDataSetChanged();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        if("CONTRIBUTION".equals(rankType)) {
            rankAdapter = new RankAdapter(null, 2);
        } else if("ATTRACTIVENESS".equals(rankType)) {
            rankAdapter = new RankAdapter(null, 1);
        }
        recyclerview.setAdapter(rankAdapter);
        rankAdapter.setOnItemClickListener(this);
        rankAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rankAdapter.isFirstOnly(true);
        swipeRefreshLayout.setOnRefreshListener(this);
        rankAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFirstVisible() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
                onRefresh();
            }
        }, 500);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if(!Utils.isLogin()){
            startActivity(LoginRegActivity.class);
            return;
        }
        UserInfo userInfo = ((RankAdapter)adapter).getUserInfoList().get(position);
        Intent intent = new Intent(getActivity(), UserInfoActivity.class);
        intent.putExtra("userinfo", userInfo);
        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        getData();
    }
}
